package com.zhen.lucene.meetlucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;

public class Searcher {

    public static void main(String[] args) throws IOException, ParseException {
        String indexDir = "lucene-in-action-1/src/main/resources/index";
        String q = "patent";
        search(indexDir,q);
    }

    private static void search(String indexDir,String q) throws IOException, ParseException {
        Directory dir = FSDirectory.open(new File(indexDir));

        IndexSearcher indexSearcher = new IndexSearcher(dir);
        QueryParser parser = new QueryParser(Version.LUCENE_30,"contents",new StandardAnalyzer(Version.LUCENE_30));
        Query query = parser.parse(q);

        long start = System.currentTimeMillis();
        TopDocs hits = indexSearcher.search(query,10);
        long end = System.currentTimeMillis();

        System.out.println("Found " + hits.totalHits + " document(s) (in " + (end - start) + " milliseconds) that matched query '" + q + "':");

        for(ScoreDoc scoreDoc : hits.scoreDocs){
            Document doc = indexSearcher.doc(scoreDoc.doc);
            System.out.println(doc.get("fullpath"));
        }

        indexSearcher.close();

    }

}
